package com.xcity.game.activity2.singlerecharge.model;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

public class SingleRechargeData {

	/** 活动序号 */
	@JSONField(name = "0")
	public int serial;
	/** 充值领奖记录 */
	@JSONField(name = "1")
	public List<Record> records;
	
	public SingleRechargeData() {}
	
	public SingleRechargeData(int serial) {
		this.serial = serial;
	}
	
	public Record lookupOrCreate(int money) {
		Record r = lookup(money);
		return r != null ? r : newRecord(money);
	}
	
	public Record lookup(int money) {
		final List<Record> list = records;
		if (list == null) {
			return null;
		}
		for (Record r : list) {
			if (r.money == money) {
				return r;
			}
		}
		return null;
	}
	
	public Record newRecord(int money) {
		if (records == null) {
			records = new ArrayList<Record>();
		}
		Record r = new Record(money);
		records.add(r);
		return r;
	}
	
	// { serial: x, records: [ { money: x, state: x } ] } ?
	// { serial: x, records: { money1: state1, money2: state2 } } ?
	public static class Record {
		/** 充值档位金额 */
		@JSONField(name = "0")
		public int money;
		/** 该档位状态（可领取次数=state >> 16 & 0xff，已领取次数=state & 0xff） */
		@JSONField(name = "1")
		public int state;
		
		public Record() {}
		
		public Record(int money) {
			this.money = money;
		}
		
		/** 获取可领奖次数 */
		public int getAvailableNum() {
			return state >> 16 & 0xff;
		}
		
		/** 获取已领奖次数 */
		public int getReceivedNum() {
			return state & 0xff;
		}
		
		public void fireRecharged() {
			int n1 = getAvailableNum() + 1;
			int n2 = getReceivedNum();
			state = ((n1 & 0xff) << 16) | (n2 & 0xff);
		}
		
		public void fireReceived() {
			int n1 = getAvailableNum();
			if (n1 > 0) --n1;
			int n2 = getReceivedNum() + 1;
			state = ((n1 & 0xff) << 16) | (n2 & 0xff);
		}
		
	}
	
}
